package com.ssy.lingxi.system.controller;

import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.system.dto.request.*;
import com.ssy.lingxi.system.dto.response.MenuListByRoleIdResponse;
import com.ssy.lingxi.system.dto.response.MenuMemberDetailsResponse;
import com.ssy.lingxi.system.dto.response.MenuResponse;
import com.ssy.lingxi.system.model.request.MenuSourceVO;
import com.ssy.lingxi.system.service.MenuService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @menu 会员角色设置-平台菜单管理
 * @author wrc
 * @date 2020/7/20
 */
@RestController
public class MenuController {

    @Resource
    private MenuService menuService;

    /**
     * 平台菜单管理- 查询权限菜单列表
     */
    @GetMapping("/menu/list")
    public Wrapper<List<MenuResponse>> menuList(@Valid MenuSourceVO sourceVO) {
        return menuService.menuList(sourceVO);
    }

    /**
     * 平台菜单管理-添加权限菜单
     *
     * @param request            接口参数
     * @param httpServletRequest Http请求
     * @return 操作结果
     */
    @PostMapping("/menu/add")
    public Wrapper<Void> menuAdd(@RequestBody @Valid MenuAddRequest request, HttpServletRequest httpServletRequest) {
        synchronized (MenuController.class) {
            return menuService.menuAdd(httpServletRequest, request);
        }
    }

    /**
     * 会员角色设置-修改权限菜单
     *
     * @param request            接口参数
     * @param httpServletRequest Http请求
     * @return 操作结果
     */
    @PostMapping("/menu/update")
    public Wrapper<Void> menuUpdate(@RequestBody @Valid MenuUpdateRequest request, HttpServletRequest httpServletRequest) {
        synchronized (MenuController.class) {
            return menuService.menuUpdate(request, httpServletRequest);
        }
    }

    /**
     * 平台菜单管理-删除权限菜单
     *
     * @param request 接口参数
     * @return 操作结果
     */
    @PostMapping("/menu/delete")
    public Wrapper<Void> menuDelete(@RequestBody MenuDeleteRequest request) {
        synchronized (MenuController.class) {
            return menuService.menuDelete(request);
        }
    }

    /**
     * 以树形菜单形式查询菜单列表（与会员服务登陆接口返回的权限菜单列表相同结构）
     *
     * @param request 接口参数
     * @return 查询结果
     */
    @GetMapping("/menu/tree")
    public Wrapper<List<Map<String, Object>>> treeMenuAsMemberLogin(@Valid MenuSourceVO request) {
        return menuService.treeMenuAsMemberLogin(request);
    }

    /**
     * 会平台菜单管理-查询菜单详情
     *
     * @param request            接口参数
     * @param httpServletRequest Http请求
     * @return 查询结果
     */
    @GetMapping("/menu/details")
    public Wrapper<MenuMemberDetailsResponse> menuDetails(MenuMemberDetailsRequest request, HttpServletRequest httpServletRequest) {
        return menuService.menuDetails(request, httpServletRequest);
    }

    /**
     * 会员角色设置-设置角色权限-菜单
     *
     * @param request 接口参数
     * @return 操作结果
     */
    @PostMapping("/menu/role/binding")
    public Wrapper<Void> menuRoleBinding(@RequestBody MenuBindingRoleRequest request) {
        synchronized (MenuController.class) {
            return menuService.menuRoleBinding(request);
        }
    }

    /**
     * 会员角色设置-设置角色权限-按钮
     *
     * @param request 接口参数
     * @return 操作结果
     */
    @PostMapping("/menu/role/binding/button")
    public Wrapper<Void> menuRoleBindingButton(@RequestBody MenuRoleBindingButtonRequest request) {
        synchronized (MenuController.class) {
            return menuService.menuRoleBindingButton(request);
        }
    }

    /**
     * 向会员服务同步指定角色的权限配置
     * @param request 角色Id
     * @return 同步结果
     */
    @RequestMapping(value = "/menu/role/auth/sync", method = RequestMethod.POST)
    public Wrapper<Void> syncRoleBinding(@RequestBody MenuRequest request) {
        synchronized (MenuController.class) {
            return menuService.syncRoleBinding(request);
        }
    }

    /**
     * 会员角色设置-查询会员的按钮集合
     *
     * @param request 接口参数
     * @return 查询结果
     */
    @GetMapping("/menu/role/button/list")
    public Wrapper<MenuListByRoleIdResponse> menuRoleButtonList(MenuRoleButtonListRequest request) {
        return menuService.menuRoleButtonList(request);
    }

    /**
     * 会员角色设置-根据角色ID查询权限菜单列表
     *
     * @param httpServletRequest Http请求
     * @param request            接口参数
     * @return 操作结果
     */
    @GetMapping("/menu/role/list")
    public Wrapper<MenuListByRoleIdResponse> menuListByRoleId(HttpServletRequest httpServletRequest, MenuRequest request) {
        return menuService.menuListByRoleId(httpServletRequest, request);
    }
}
